package hadoop.hdfs.demo;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author qiss
 * @create 2018-02-12 17:08
 **/
@Data
@NoArgsConstructor
public class BeanComparable implements WritableComparable<BeanComparable> {
    private int sum1;
    private int sum2;
    private int total;

    public BeanComparable(int sum1,int sum2){
        super();
        this.sum1 = sum1;
        this.sum2 = sum2;
        this.total = sum1+sum2;
    }


    @Override
    public int compareTo(BeanComparable o) {
        return total>o.getTotal()?-1:1; //倒序
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(sum1);
        out.writeInt(sum2);
        out.writeInt(sum1+sum2);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        sum1=in.readInt();
        sum2=in.readInt();
        total=in.readInt();
    }
    @Override
     public String toString(){
         return sum1+"---"+sum2+"---"+total;
     }
}
